//
//  EmotionAttribute.swift
//  01-表情键盘
//
//  Created by mac on 16/4/22.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class EmotionAttribute: NSTextAttachment {

    var emotion:Emoticon
    
    init(emotion:Emoticon){
        
        self.emotion = emotion
        
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func imageText(font:UIFont)->NSAttributedString{
        
        //设置图片
        image = UIImage(contentsOfFile: emotion.imageUrl)
        
        //记录线高，使文字和图片一样大小
        let lineHeight = font.lineHeight
        //设置大小
        bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: self))
        
        //添加字体
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0,length: 1))
    
        return imageText
    }
    
}
